package com.yupi.project.bizmq;

import com.rabbitmq.client.Channel;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.model.entity.Chart;
import com.yupi.project.service.impl.ZhiPuAIClient;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
@Slf4j
public class BIMessageConsumer {
    @Resource
    private ZhiPuAIClient zhiPuAIClient;
    @SneakyThrows
    @RabbitListener(queues = BIConstant.BI_QUEUE_NAME,ackMode = "MANUAL")
    public void receive(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG)long deliveryTag) {
        log.info("receive message: " + msg);
        String result = zhiPuAIClient.callAI(msg.toString());
        System.out.println(result);
        String[] split = result.split("【【【【【");
        String chartCode = "";
        String genResult = "";
        if (split[1] != null){
            chartCode = split[1];
            System.out.println(split[1].trim());
        }
        if (split[2] != null){
            genResult = split[2];
            //System.out.println(split[2].trim());
        }
//        //存储再从数据库中
//        Chart chart = new Chart();
//        chart.setName("");
//        chart.setGoal(goal);
//        chart.setChartData(csvResult);
//        chart.setChatType("");
//        chart.setGenChart(chartCode);
//        chart.setGenResult(genResult);
//        chart.setUserId(null);
//        boolean save = ChartService.save(chart);
//        if (!save) {
//            throw new BusinessException(ErrorCode.OPERATION_ERROR,"数据库保存失败");
//        }
        channel.basicAck(deliveryTag,false);
    }
}
